import { useState } from "react";
import { View, Text, Image, WebView } from "@tarojs/components";
import Taro, { useDidShow, useLoad } from "@tarojs/taro";
import { iconMineInfo, iconRightGray } from "../../assets/files.js";

import "./index.scss";
import { Switch } from "@nutui/nutui-react-taro";


export default function MessageSetting() {

  const [checkedAsync, setCheckedAsync] = useState(true)
  const [moduleId, setModuleId] = useState("ci15Jz2DJ6ZNkLVVJCATmZv3YZO3yI0BJ4fDE0Gog-4")

  const onChangeAsync = (value, event) => {
    // setCheckedAsync(!checkedAsync)
    dingyue()
  }

  // useDidShow(()=>{
  //   dingyue()
  // })

  const dingyue = () => {
    Taro.requestSubscribeMessage({
      tmplIds: [moduleId],
      success(res) {
        console.log(res);
        // console.log(res[moduleId]);
        if (res[moduleId] === 'reject') {
          // toSetting()
          // test()
          // test2()
        }
        // setCheckedAsync(true)
      },
      fail(err) {
        console.log(err);

      },
    })
  }
  const test2 = () =>{
    Taro.openSetting({
      withSubscriptions:true,
      success(res){
        console.log(res.subscriptionsSetting?.mainSwitch)
        if(res.subscriptionsSetting?.mainSwitch){
          setCheckedAsync(true)
        }else{
          setCheckedAsync(false)
        }
        // dingyue()
      },
      fail(err){
        console.log(err);
      }
    })
  }

  const test = () =>{
    Taro.getSetting({
      withSubscriptions: true,
      success: res => {
        let {
          authSetting = {},
          subscriptionsSetting: { mainSwitch = false, itemSettings = {} } = {}
        } = res;

        if (
          (authSetting['scope.subscribeMessage'] || mainSwitch) &&
          itemSettings[moduleId] === 'accept'
        ) {
          // this.submitClock();
          console.log('用户手动开启同意了，订阅消息');
        } else {
          // this.$invoke('Toast', '__warning__', [
          //   `您没有同意授权订阅消息，预约领取失败`
          // ]);
        }
      }
    });
  }

  const toSetting = () => {
    Taro.getSetting({
      withSubscriptions: true,
      success (res) {
        console.log(res.authSetting)
        // res.authSetting = {
        //   "scope.userInfo": true,
        //   "scope.userLocation": true
        // }
        console.log(res.subscriptionsSetting)
        // res.subscriptionsSetting = {
        //   mainSwitch: true, // 订阅消息总开关
        //   itemSettings: {   // 每一项开关
        //     SYS_MSG_TYPE_INTERACTIVE: 'accept', // 小游戏系统订阅消息
        //     SYS_MSG_TYPE_RANK: 'accept'
        //     zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 普通一次性订阅消息
        //     ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
        //   }
        // }
      }
    })
  }

  return (
    <View className="wrap">


      <View className="mainItemWrap">
        <View
          className="item"
          onClick={() => { }}
        >
          <Text className="itemDes">审核消息</Text>
          <Switch
            checked={checkedAsync}
            // onChange={(value, event) => onChangeAsync(value, event)}
            onChange={(value, event) => test2()}
          />
        </View>

      </View>


    </View>
  );
}
